<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">

  <xi:include href="master.html" />

<head>
  <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
  <title>MunkiReport</title>
</head>

<body>

<?python
    sort_flip = u'false' if reverse else u'true'
    sort_keep = u'true' if reverse else u'false'
    sort_name = u'order_by=name&reverse=' + (sort_flip if order_by == u'name' else sort_keep)
    sort_user = u'order_by=user&reverse=' + (sort_flip if order_by == u'user' else sort_keep)
    sort_addr = u'order_by=addr&reverse=' + (sort_flip if order_by == u'addr' else sort_keep)
    sort_time = u'order_by=time&reverse=' + (sort_flip if order_by == u'time' else sort_keep)
    triangle = u'\u25bc' if reverse else u'\u25b2'
    triangle_name = triangle if order_by == u'name' else u''
    triangle_user = triangle if order_by == u'user' else u''
    triangle_addr = triangle if order_by == u'addr' else u''
    triangle_time = triangle if order_by == u'time' else u''
?>


<h2>Errors</h2>

<py:choose test="len(error_clients)">
  <py:when test="0">
    <p><i>No errors.</i></p>
  </py:when>
  <py:otherwise>
    <table class="clientlist">
      <thead>
        <tr>
          <th><a class="sortheader" href="?${sort_name}">Client     ${triangle_name}</a></th>
          <th><a class="sortheader" href="?${sort_user}">User       ${triangle_user}</a></th>
          <th><a class="sortheader" href="?${sort_addr}">IP         ${triangle_addr}</a></th>
          <th><a class="sortheader" href="?${sort_time}">Latest Run ${triangle_time}</a></th>
        </tr>
      </thead>
      <tbody>
        <tr py:for="client in error_clients">
          <?python
              url = tg.url('/view/report/%s' % client.mac)
          ?>
          <td>
            <py:choose test="client.report_plist == None">
              <py:when test="False">
                <a href="${url}">${client.name}</a>
              </py:when>
              <py:otherwise>${client.name}</py:otherwise>
            </py:choose>
          </td>
          <td py:content="client.console_user">user</td>
          <td py:content="client.remote_ip" class="ip">ip</td>
          <td>
            ${client.timestamp.strftime("%Y-%m-%d %H:%M")}
            ${client.runtype}
            ${client.runstate}
            <py:if test="client.errors">
              <span class="error">
                <a href="${url}#errors">
                  ${client.errors} error${"s" if client.errors > 1 else ""}
                </a>
              </span>
            </py:if>
            <py:if test="client.warnings">
              <span class="warning">
                <a href="${url}#errors">
                  ${client.warnings} warning${"s" if client.warnings > 1 else ""}
                </a>
              </span>
            </py:if>
          </td>
        </tr>
      </tbody>
    </table>
  </py:otherwise>
</py:choose>


<h2>Warnings</h2>

<py:choose test="len(warning_clients)">
  <py:when test="0">
    <p><i>No warnings.</i></p>
  </py:when>
  <py:otherwise>
    <table class="clientlist">
      <thead>
        <tr>
          <th><a class="sortheader" href="?${sort_name}">Client${triangle_name}</a></th>
          <th><a class="sortheader" href="?${sort_user}">User${triangle_user}</a></th>
          <th><a class="sortheader" href="?${sort_addr}">IP${triangle_addr}</a></th>
          <th><a class="sortheader" href="?${sort_time}">Latest Run${triangle_time}</a></th>
        </tr>
      </thead>
      <tbody>
        <tr py:for="client in warning_clients">
          <?python
              url = tg.url('/view/report/%s' % client.mac)
          ?>
          <td>
            <py:choose test="client.report_plist == None">
              <py:when test="False">
                <a href="${url}">${client.name}</a>
              </py:when>
              <py:otherwise>${client.name}</py:otherwise>
            </py:choose>
          </td>
          <td py:content="client.console_user">user</td>
          <td py:content="client.remote_ip" class="ip">ip</td>
          <td>
            ${client.timestamp.strftime("%Y-%m-%d %H:%M")}
            ${client.runtype}
            ${client.runstate}
            <py:if test="client.errors">
              <span class="error">
                <a href="${url}#errors">
                  ${client.errors} error${"s" if client.errors > 1 else ""}
                </a>
              </span>
            </py:if>
            <py:if test="client.warnings">
              <span class="warning">
                <a href="${url}#errors">
                  ${client.warnings} warning${"s" if client.warnings > 1 else ""}
                </a>
              </span>
            </py:if>
          </td>
        </tr>
      </tbody>
    </table>
  </py:otherwise>
</py:choose>


<h2>Activity</h2>

<py:choose test="len(activity_clients)">
  <py:when test="0">
    <p><i>No active clients.</i></p>
  </py:when>
  <py:otherwise>
    <table class="clientlist">
      <thead>
        <tr>
          <th><a class="sortheader" href="?${sort_name}">Client${triangle_name}</a></th>
          <th><a class="sortheader" href="?${sort_user}">User${triangle_user}</a></th>
          <th><a class="sortheader" href="?${sort_addr}">IP${triangle_addr}</a></th>
          <th><a class="sortheader" href="?${sort_time}">Latest Run${triangle_time}</a></th>
        </tr>
      </thead>
      <tbody>
        <tr py:for="client in activity_clients">
          <?python
              url = tg.url('/view/report/%s' % client.mac)
          ?>
          <td>
            <py:choose test="client.report_plist == None">
              <py:when test="False">
                <a href="${url}">${client.name}</a>
              </py:when>
              <py:otherwise>${client.name}</py:otherwise>
            </py:choose>
          </td>
          <td py:content="client.console_user">user</td>
          <td py:content="client.remote_ip" class="ip">ip</td>
          <?python
              install_items   = len(client.activity['ItemsToInstall'])  if 'ItemsToInstall'  in client.activity else 0
              install_results = len(client.activity['InstallResults'])  if 'InstallResults'  in client.activity else 0
              removal_items   = len(client.activity['ItemsToRemove'])   if 'ItemsToRemove'   in client.activity else 0
              removal_results = len(client.activity['RemovalResults'])  if 'RemovalResults'  in client.activity else 0
              apple_updates   = len(client.activity['AppleUpdateList']) if 'AppleUpdateList' in client.activity else 0
              pending_installs = max((install_items + apple_updates) - install_results, 0)
              pending_removals = removal_items - removal_results
          ?>
          <td>
            ${client.timestamp.strftime("%Y-%m-%d %H:%M")}
            &nbsp;
            <py:if test="'Updating' in client.activity">
              ${client.runtype} run in progress
            </py:if>
            <py:if test="pending_installs">
              ${pending_installs} pending install${"s" if pending_installs > 1 else ""}
            </py:if>
            <py:if test="install_results">
              ${install_results} package${"s" if install_results > 1 else ""} installed
            </py:if>
            <py:if test="pending_removals">
              ${pending_removals} pending removal${"s" if pending_removals > 1 else ""}
            </py:if>
            <py:if test="removal_results">
              ${removal_results} package${"s" if removal_results > 1 else ""} removed
            </py:if>
          </td>
        </tr>
      </tbody>
    </table>
  </py:otherwise>
</py:choose>

<p><i>No active packages.</i></p>


<h2>Client List</h2>

<ul>
  <li><a href="${tg.url('/view/client_list')}">View all clients</a></li>
</ul>

</body>
</html>
